<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Promise extends UserCommonController
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('MOrder');
        $this->load->model('MUserPromise');
    }

    public function index(){
        $data = array();
        $data['promise'] = [
          'promise_amount' => $this->siteConf->promise_amount,
          'promise_rate' => $this->siteConf->promise_rate,
        ];
        if ($this->user->promise_status){
            $promise = $this->MUserPromise->getOne(['user_id' => $this->user->id, 'status <>' => 0]);
            $data['promise']['user_promise'] = $promise;
        }
        $data['user'] = $this->user;
        $this->ReturnJson(200,true,'success',$data);
    }

    public function add(){
        $promise = [
            'user_id' => $this->user->id,
            'amount' => $this->param['amount'],
            'create_time' => time(),
            'update_time' => time()
        ];
        $pid = $this->MUserPromise->add((object)$promise);
        if ($pid){
            $this->ReturnJson(200,true,'success',$pid);
        }else{
            $this->ReturnJson(500,false,'fail',$promise);
        }
    }
}
